package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.user.user;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText username;
    private EditText password;
    private CheckBox savePws;
    private SharedPreferences share;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        Button login = findViewById(R.id.login);
        login.setOnClickListener(this);
        Button register = findViewById(R.id.register);
        register.setOnClickListener(this);
        savePws = findViewById(R.id.savePwd);
        //存储数据
        share = getSharedPreferences("login", 0);
        String username2 = share.getString("username",null);
        String password2 = share.getString("password",null);
        if (username2 != null){
            username.setText(username2);
        }
        if (password2 != null){
            password.setText(password2);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(username.getWindowToken(),0);
        return super.onTouchEvent(event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                OkHttpClient okHttpClient = new OkHttpClient();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("username",username.getText());
                    jsonObject.put("password",password.getText());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                RequestBody requestBody = RequestBody.create(jsonObject.toString(), MediaType.parse("application/json"));
                Request build = new Request.Builder().url("https://mock.w2code.com/api/login").post(requestBody).build();
                okHttpClient.newCall(build).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String string = response.body().string();
                        user user = new Gson().fromJson(string, user.class);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (user.data.verifySuccess){
                                    if (savePws.isChecked()){
                                        SharedPreferences.Editor edit = share.edit();
                                        edit.putString("username",username.getText().toString());
                                        edit.putString("password",password.getText().toString());
                                        edit.commit();
                                    }
                                    //弹出欢迎语
                                    Toast.makeText(MainActivity.this,user.data.userInfo.username+",欢迎你",Toast.LENGTH_SHORT).show();
                                    //页面跳转啊
                                    startActivity(new Intent(MainActivity.this,MainActivity2.class));
                                }else {
                                    Toast.makeText(MainActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
                                    password.setText("");
                                }
                            }
                        });
                    }
                });
                break;
            case R.id.register:
                //跳转到注册
                startActivity(new Intent(MainActivity.this,Register.class));
                break;
        }
    }
}